var ArchiveMaterials = {
    tableId: "archiveMaterialsTable"
};
layui.use(['table', 'admin', 'ax','form','func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;
    /**
     * 档案资料信息表管理
     */


    /**
     * 初始化表格的列
     */
    ArchiveMaterials.initColumn = function () {
        return [[
            {field: 'xh',width:80, fixed: 'left', sort: true, title: '序号'},
            {field: 'wh',width:80, sort: true, title: '文号'},
            {field: 'zlmc',width:160, title: '资料名称'},
            {field: 'zllx',   width:120, title: '资料类型',templet:'#zllx'},
            {field: 'qspzh',  width:120, title: '起始凭证号'},
            {field: 'jspzh',  width:120, title: '结束凭证号'},
            {field: 'qsym',   width:120, title: '起始页码'},
            {field: 'jzym',   width:120, title: '截止页码'},
            {field: 'nd',     width:120, title: '年度'},
            {field: 'sfyx',   width:120, title: '是否有效',templet:'#sfyx'},
            {field: 'beiz', title: '备注'},
            {fixed:'right',width:120,align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    ArchiveMaterials.search = function (wheres) {
        table.reload(ArchiveMaterials.tableId, {
            where: wheres, page: {curr: 1}
        });
    };


    //表单提交事件
    form.on('submit(btnSearch)', function (data) {
        ArchiveMaterials.search(data.field);
        return false;
    });

    /**
     * 弹出添加对话框
     */
    ArchiveMaterials.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/archiveMaterials/add';
    };

    /**
     * 导出excel按钮
     */
    ArchiveMaterials.exportExcel = function () {
        var checkRows = table.checkStatus(ArchiveMaterials.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ArchiveMaterials.openInfoDlg = function (data) {
        // window.location.href = Feng.ctxPath + '/archiveMaterials/info?id=' + data.id;
        func.open({
            height: 900,
            width: 900,
            title: '查看档案资料',
            content: Feng.ctxPath + '/archiveMaterials/info?id=' + data.id
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ArchiveMaterials.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/archiveMaterials/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ArchiveMaterials.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ArchiveMaterials.tableId,
        url: Feng.ctxPath + '/archiveMaterials/list',
        page: true,
        height: "full-100",
        cellMinWidth: 100,
        cols: ArchiveMaterials.initColumn()
    });

    // 导出excel
    $('#btnExp').click(function () {
        ArchiveMaterials.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ArchiveMaterials.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'info') {
            ArchiveMaterials.openInfoDlg(data);
        }
    });
});
